<?php
class M_daftarkegiatan extends CI_Model {
	
	function list_kegiatan($filter,$start,$end){
		$sql = "SELECT kegiatan_id, kegiatan_keterangan, kegiatan_date , anggaran_kegiatan FROM t_kegiatan
				LEFT JOIN m_jeniskegiatan ON m_jeniskegiatan_id =jeniskegiatan_id
				LEFT JOIN m_anggaran ON m_anggaran_id = anggaran_id ";
		$query = $this->db->query($sql);
		$total = $query->num_rows();
		
		if ($filter<>""){
				$sql .=preg_match("/WHERE/i",$sql)? " AND ":" WHERE ";
				$sql .= " (lower(kegiatan_keterangan) LIKE '%".addslashes(strtolower($filter))."%' )";
		}
		$sql .= " ORDER BY kegiatan_id";
		$limit = $sql." LIMIT ".$start.", ".$end;
		$query = $this->db->query($limit);
		$arr = array();
		foreach($query->result() as $q){
			$arr[] = $q; 
		}
	
		$final = array(
			'total'   => $total,
			'results' => $arr
		);
		return json_encode($final);
	}
	
	function list_kegiatandokumen($parent,$filter,$start,$end){
		$sql = "SELECT kegiatandokumen_id ,kegiatandokumen_nama ,dokumen_nama 
				  FROM t_kegiatandokumen
				LEFT JOIN t_kegiatan ON t_kegiatan_id = kegiatan_id
				LEFT JOIN m_dokumen ON m_dokumen_id = dokumen_id ";
		if ($parent <> ""){
				$sql .=preg_match("/WHERE/i",$sql)? " AND ":" WHERE ";
				$sql .= " (lower(t_kegiatan_id) = '".addslashes($parent)."' )";
		}
		$query = $this->db->query($sql);
		$total = $query->num_rows();
		
		if ($filter<>""){
				$sql .=preg_match("/WHERE/i",$sql)? " AND ":" WHERE ";
				$sql .= " (lower(kegiatandokumen_id) LIKE '%".addslashes(strtolower($filter))."%' )";
		}
		$sql .= " ORDER BY kegiatandokumen_id";
		$limit = $sql." LIMIT ".$start.", ".$end;
		$query = $this->db->query($limit);
		$arr = array();
		foreach($query->result() as $q){
			$arr[] = $q; 
		}
	
		$final = array(
			'total'   => $total,
			'results' => $arr
		);
		return json_encode($final);
	}
	
	function list_peserta($parent,$filter,$start,$end){
		$sql = "SELECT kegiatanperson_id , personil_nama ,kegiatanperson_jabatan FROM t_kegiatanperson
				LEFT JOIN m_personil ON m_personil_id = personil_id
				";
		$query = $this->db->query($sql);
		$total = $query->num_rows();
		if ($parent <> ""){
				$sql .=preg_match("/WHERE/i",$sql)? " AND ":" WHERE ";
				$sql .= " (lower(t_kegiatandokumen_id) = '".addslashes($parent)."' )";
		}
		
		if ($filter<>""){
				$sql .=preg_match("/WHERE/i",$sql)? " AND ":" WHERE ";
				$sql .= " (lower(kegiatanperson_id) LIKE '%".addslashes(strtolower($filter))."%' )";
		}
		$sql .= " ORDER BY kegiatanperson_id";
		$limit = $sql." LIMIT ".$start.", ".$end;
		$query = $this->db->query($limit);
		$arr = array();
		foreach($query->result() as $q){
			$arr[] = $q; 
		}
	
		$final = array(
			'total'   => $total,
			'results' => $arr
		);
		return json_encode($final);
	}
	
	function delete($kegiatan_id){
		$sql = "DELETE FROM t_kegiatan WHERE kegiatan_id = '".$kegiatan_id."'";
		$this->db->query($sql);
		$sql = "DELETE FROM t_kegiatandokumen WHERE t_kegiatan_id = '".$kegiatan_id."'";
		$this->db->query($sql);
		$sql = "DELETE FROM t_kegiatanperson WHERE t_kegiatan_id = '".$kegiatan_id."'";
		$this->db->query($sql);
	}
}